package com.geez.bbl.util;


import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: biniam.gebremichael
 * Date: Jul 15, 2010
 */
public class FileUtils {
    public static int processFile(File file, Consumer<String> consumer) {
        BufferedReader br = null;
        int lineCounter = 0;
        try {
            br = new BufferedReader(
                    new InputStreamReader(new FileInputStream(file), "UTF-8"));

            String line = br.readLine();
            while (line != null) {
                if (line.trim().length() != 0) {
                    lineCounter++;
                    consumer.consume(line);
                }

                line = br.readLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return lineCounter;
    }

    public static List<String> readFileToListOfLines(File file) {
           final List<String> result = new ArrayList<String>();
           try {
               processFile(file, new Consumer<String>() {
                   @Override
                   public void consume(String s) {
                       result.add(s.trim());
                   }
               });
           } catch (Exception e) {
               e.printStackTrace();
           }
           return result;
       }

    public static String getFileContent(File file) {
          return getFileContent(file,true);
    }
    public static String getFileContent(File file, final boolean addNewLine) {
       // final AtomicBoolean chapStarted = new AtomicBoolean(false);
        final StringBuilder builder = new StringBuilder();
        processFile(file, new Consumer<String>() {
            @Override
            public void consume(String message) {
           //     if (chapStarted.get()) {
                builder.append(message) ;
                if(addNewLine){
                       builder.append("\n");
                }
            }
        });
        return builder.toString();

    }


    public static Map<String, String> processCSV(File file) {
        final Map<String, String> csv = new HashMap<String, String>();
        processFile(file, new Consumer<String>() {
            @Override
            public void consume(String message) {
                String[] a = message.split(",",2);
                if(a.length==2){
                csv.put(a[0], a[1]);
                }else{
                    System.out.println("short cvs line = " + message);
                }
            }
        });
        return csv;

    }
   
    public synchronized static Map<String, Map<Integer, Integer>> processExpectedVerse(File file) {
        final Map<String, Map<Integer, Integer>> csv = new HashMap<String, Map<Integer, Integer>>();
        processFile(file, new Consumer<String>() {
            @Override
            public void consume(String message) {
                String[] a = message.split(",");

                Map<Integer, Integer> b ;
                if (csv.containsKey(a[0]))                {
                    b = csv.get(a[0]);
                } else {
                    b = new HashMap<Integer, Integer>();
                }
                b.put(Integer.valueOf(a[1].trim()), Integer.valueOf(a[2].trim()));
                csv.put(a[0], b);
            }
        });
        return csv;

    }

    public static void save(File fileName, String xml) {
        try {
            Writer writer = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(fileName), "UTF-8"));
            writer.append(xml);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
